// Geometric Tools, LLC
// Copyright (c) 1998-2010
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)
#if defined(_WIN32) || defined(WIN32) 
	#define OS_WIN
#endif
//#define MULTIPLAYERMODE 0;//comment this out not to use multiplayer
#if defined (MULTIPLAYERMODE)
	void CheckMsg(char *recvbuf);
	
	#define DEFAULT_BUFLEN 512

	#include "Network.h"

	#ifdef OS_WIN
	//Windows specific stuff
	#define OS 0
	WinSockNetwork network;
	#else
	//Mac related stuff
	#define OS 1
	BSDNetwork network;
#include <pthread.h>
	#endif
#endif

#ifndef SquirrelAdven_H
#define SquirrelAdven_H

#include "Wm5WindowApplication3.h"
#include "Map.h"
#include "NPCList.h"
#include "PlayerList.h"
#include <iostream>
#include <string>

using namespace Wm5;


class SquirrelAdven : public WindowApplication3
{
	WM5_DECLARE_INITIALIZE;
	WM5_DECLARE_TERMINATE;

public:
	SquirrelAdven ();

	virtual bool OnInitialize ();
	virtual void OnTerminate ();
	virtual void OnIdle ();
	virtual bool OnKeyDown (unsigned char key, int x, int y);
	inline virtual bool OnKeyUp (unsigned char key, int x, int y);
	virtual bool OnSpecialKeyDown (int key, int x, int y);
	inline virtual bool OnSpecialKeyUp (int key, int x, int y);
	virtual bool OnMouseClick (int button, int state, int x, int y,
		unsigned int modifiers);


protected:
	void CreateScene ();
	void UpdateSquirrelAdven ();
	void CreateHUD();
	void CreateRoom();
	void CreateMap();
	void CreateNPC();
	void CreatePlayer();
	void CreateItem();
	void CreateIntroHUD();
	void CreateLights ();
	void UpdateHUD();
	void startBackgroundMusic();

	Float4 mTextColor;

	// scene
	NodePtr mScene;
	Culler mCuller;

	// The scene has a directional light.
	NodePtr mDLightRoot;
	LightPtr mDLight1, mDLight2;
	
	bool gameEnd;

	Map * gameMap;
	NPCList * npcList;
	ItemList * itemList;
	PlayerList * playerList;
	Node * playerNodes[10];
	Node * items;

	int numPlayer;
	int selfPlayerID;

	float moveSpeed;
	float rotSpeed;
	
	bool dialogMode;
	int Intro;
	bool showMap;

	ofstream outFile; // object for writing to a file SINCE PRINTFs AND COUTs DONT WORK
	bool isTyping;
	char* chatMessage;
	int chatCounter;
	char ServerMessageToScreen[1024];
	
	char* ServerMessage;

	char mPickMessage[1024];	// for debugging
	std::string mInventoryMessage;
	std::string mNPCMessage_npc;
	std::string mNPCMessage_option;
	std::string mNPCMessage_player;
	
	CameraPtr mScreenCamera;//used for HUD
	TriMeshPtr mForePoly,mForePlayer,mIntro,mIntro2,mIntro3,mIntro4,mIntro5,mEnd;
	TriMeshPtr npcShadedAreas;
	float mPlayerPos_x,mPlayerPos_y;
	
	NPC *curTalkingNPC;
	DialogNode * curDialogNode;
	std::vector<int> talkedNPC;
	int option;
	
	bool onLeftKeyDown;
	bool onRightKeyDown;
	bool onUpKeyDown;
	bool onDownKeyDown;
	
	bool leftKeyDown(int playerID);
	bool rightKeyDown(int playerID);
	bool upKeyDown();
	bool downKeyDown();
	
	void moveForward(int playerID);
	void moveBackward(int playerID);
	void pickUpItem(Player * curPlayer);
	void talkWithNPC(Player * curPlayer);
	
	// collision
	// The collision system.
	Picker mPicker;
	bool AllowMotion (float sign);
	Node* gamemap;

	#if defined (MULTIPLAYERMODE)
		void CheckMsg(char *recvbuf);
		#ifdef OS_WIN
		static int ThreadFunc(void* pThis);
		int Process();
		#else
		    static void *listen_helper(void *context);
			void *listen(void);
		#endif
	#endif
	
};

WM5_REGISTER_INITIALIZE(SquirrelAdven);
WM5_REGISTER_TERMINATE(SquirrelAdven);

#endif

